package com.example.firstapp;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class ListviewActivity extends AppCompatActivity {

    //标题
    private final String[] titles = {"桌子", "苹果", "蛋糕", "线衣", "猕猴桃", "围巾", "兔子", "香蕉"};
    //价格
    private final String[] prices = {"1800元", "10元/kg", "300元", "350元", "10元/kg", "280元", "88元", "6元/kg"};
    //图片
    private final int[] icons = {R.drawable.table, R.drawable.apple, R.drawable.cake,
            R.drawable.wireclothes, R.drawable.kiwifruit, R.drawable.scarf, R.drawable.rabbit, R.drawable.banana};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview);

        //1.获取ListView对象
        ListView listView = findViewById(R.id.lv_data);
        MyAdapter myAdapter = new MyAdapter();
        listView.setAdapter(myAdapter);

        listView.setOnItemClickListener((adapterView, view, i, id) -> {
            Toast.makeText(ListviewActivity.this, "点击了第"+i+"个"+id, Toast.LENGTH_SHORT).show();
        });
        // 一般常用长按删除
        listView.setOnItemLongClickListener((adapterView, view, i, id) -> {
            Toast.makeText(ListviewActivity.this, "点击了第"+i+"个"+id+"删除", Toast.LENGTH_SHORT).show();
            return true;
        });
    }

    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public Object getItem(int position) {
            return titles[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //优化代码
            MyViewHolder vh = null;
            if (convertView == null){
                //1.加载布局
                convertView = View.inflate(ListviewActivity.this, R.layout.item_list, null);
                //2.获取控件
                vh = new MyViewHolder();
                vh.iv_icon = convertView.findViewById(R.id.iv);
                vh.tv_title = convertView.findViewById(R.id.tv_title);
                vh.tv_price = convertView.findViewById(R.id.tv_price);
                convertView.setTag(vh);
            }else {
                vh = (MyViewHolder) convertView.getTag();
            }
            //3.设置数据
            vh.iv_icon.setImageResource(icons[position]);
            vh.tv_title.setText(titles[position]);
            vh.tv_price.setText(prices[position]);
            return convertView;
        }
    }

    class MyViewHolder{
        ImageView iv_icon;
        TextView tv_title;
        TextView tv_price;
    }
}